﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;
using HYFamilyLibDataManager;

namespace HYRElectric.Dimension
{
	internal partial class DlgWireTagInsert : System.Windows.Forms.Form
	{
		public DbFamily SelectDbFamily
		{
			get
			{
				return this.selectDbFamily;
			}
		}

		public DlgWireTagInsert(UIApplication ui_app)
		{
			this.InitializeComponent();
			this.xmlHelper.LoadXml(this.path, "Root");
			this.InitTagTypes(ui_app);
			this.InitTagText();
			this.LoadDlgData();
		}

		public string TagText
		{
			get
			{
				return this.cbxTag.Text;
			}
		}

		private void InitTagTypes(UIApplication ui_app)
		{
			this.cbxFamily.Items.Clear();
			FamilyLibDatabase familyLibDatabase = null;
			try
			{
				familyLibDatabase = FamilyLibDatabase.GetSqliteDB(SubjectType.System, ui_app.Application.VersionNumber);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			DbCategory dbCategory = familyLibDatabase.CategoryTable["电气标记"]["电线标记"]["回路标记"];
			if (dbCategory == null)
			{
				return;
			}
			foreach (DbFamily item in dbCategory.GetAllFamilies(false, CheckType.All))
			{
				this.cbxFamily.Items.Add(item);
			}
		}

		private void InitTagText()
		{
			this.cbxTag.Items.Clear();
			this.cbxTag.Items.Add("<默认>");
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireTagInsert"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireTagInsert", "Params"));
			this.xmlHelper.SetAttribute("LeadDim", this.cbxLeadDim.Checked ? "True" : "False");
			this.xmlHelper.SetAttribute("DimSymbol", this.cbxFamily.SelectedIndex.ToString());
			string text = "";
			int num = 0;
			while (num < this.cbxTag.Items.Count - 1 && num < 3)
			{
				text = string.Concat(new object[]
				{
					text,
					this.cbxTag.Items[num],
					"&",
					text
				});
				num++;
			}
			text.TrimEnd(new char[]
			{
				'&'
			});
			this.xmlHelper.SetAttribute("DimTag", text);
			string value = this.cbxTag.SelectedIndex.ToString();
			this.xmlHelper.SetAttribute("DimTagItem", value);
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireTagInsert", "Params"));
			string attribute = this.xmlHelper.GetAttribute("LeadDim");
			if (!string.IsNullOrEmpty(attribute))
			{
				if (attribute == "True")
				{
					this.cbxLeadDim.Checked = true;
				}
				else
				{
					this.cbxLeadDim.Checked = false;
				}
			}
			string attribute2 = this.xmlHelper.GetAttribute("DimSymbol");
			int num = (attribute2.Length == 0) ? 0 : Convert.ToInt32(attribute2);
			if (num < 0 || num >= this.cbxFamily.Items.Count)
			{
				this.cbxFamily.SelectedIndex = 0;
			}
			else
			{
				this.cbxFamily.SelectedIndex = num;
			}
			this.selectDbFamily = (DbFamily)this.cbxFamily.SelectedItem;
			string attribute3 = this.xmlHelper.GetAttribute("DimTag");
			string[] separator = new string[]
			{
				"&"
			};
			foreach (string text in attribute3.Split(separator, StringSplitOptions.RemoveEmptyEntries))
			{
				if (!this.cbxTag.Items.Contains(text))
				{
					this.cbxTag.Items.Insert(0, text);
					if (this.cbxTag.Items.Count >= 4)
					{
						break;
					}
				}
			}
			string attribute4 = this.xmlHelper.GetAttribute("DimTagItem");
			attribute4.Trim();
			num = ((attribute4.Length == 0) ? 0 : Convert.ToInt32(attribute4));
			if (num < 0 || num >= this.cbxTag.Items.Count)
			{
				this.cbxTag.SelectedIndex = this.cbxTag.Items.Count - 1;
				return;
			}
			this.cbxTag.SelectedIndex = num;
		}

		private void cbxTag_SelectedIndexChanged(object sender, EventArgs e)
		{
			int selectedIndex = this.cbxTag.SelectedIndex;
			if (selectedIndex == 0 || selectedIndex == this.cbxTag.Items.Count - 1)
			{
				return;
			}
			string item = this.cbxTag.SelectedItem as string;
			this.cbxTag.Items.RemoveAt(selectedIndex);
			this.cbxTag.Items.Insert(0, item);
			this.cbxTag.SelectedIndex = 0;
		}

		private void cbxTag_Leave(object sender, EventArgs e)
		{
			string text = this.cbxTag.Text;
			text.Trim();
			if (text.Length == 0)
			{
				this.cbxTag.Text = "<默认>";
				text = this.cbxTag.Text;
			}
			if (this.cbxTag.Items.Contains(text))
			{
				this.cbxTag.SelectedIndex = this.cbxTag.Items.IndexOf(text);
				return;
			}
			if (this.cbxTag.Items.Count >= 4)
			{
				for (int i = 3; i < this.cbxTag.Items.Count - 1; i++)
				{
					this.cbxTag.Items.RemoveAt(i);
				}
			}
			this.cbxTag.Items.Insert(0, text);
			this.cbxTag.SelectedIndex = 0;
		}

		private void cbxTag_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.cbxTag.Text == "<默认>")
			{
				this.cbxTag.Text = "";
			}
		}

		private void cbxTag_Enter(object sender, EventArgs e)
		{
			if (this.cbxTag.Text == "<默认>")
			{
				this.cbxTag.Text = "";
			}
		}

		private void DlgWireTagInsert_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void DlgWireTagInsert_MouseLeave(object sender, EventArgs e)
		{
			this.cbxTag_Leave(sender, e);
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				WindowHelper.SendMessageToWndRevit();
				return;
			}
			base.Focus();
		}

		private void cbxFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.selectDbFamily = (DbFamily)this.cbxFamily.SelectedItem;
		}

		private void DlgWireTagInsert_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "WireTagInsert.xml");

		private const string Root = "Root";

		private const string Node = "HYWireTagInsert";

		private const string ChildNode = "Params";

		private const string LeadDim = "LeadDim";

		private const string DimSymbol = "DimSymbol";

		private const string DimTag = "DimTag";

		private const string DimTagItem = "DimTagItem";

		private DbFamily selectDbFamily;
	}
}
